/**
 * Created by xuweijie on 17-5-6.
 */

//注意：选项卡 依赖 element 模块，否则无法进行功能性操作
layui.use('element', function () {
    var element = layui.element();
});

layui.use('form', function () {
    var form = layui.form(),
        layer = layui.layer
        ,layedit = layui.layedit
        ,laydate = layui.laydate;

    //表单
    form.verify({
        goodnumber: function(value){
            if(isNaN(value)){
                return '商品数量应为数字';
            }
        }, price: function(value){
            if(isNaN(value)){
                return '商品价格应为数字';
            }
        }
    });
    //创建一个编辑器
    layedit.build('LAY_demo_editor');

    //监听提交
    form.on('submit(formDemo)', function(data){
        layer.alert(JSON.stringify(data.field), {
            title: '最终的提交信息'
        })
        return false;
    });
});

layui.use('laydate', function() {
    var laydate = layui.laydate;

    var start = {
        min: laydate.now()
        , max: '2099-06-16 23:59:59'
        , istoday: true
        , choose: function (datas) {
            end.min = datas; //开始日选好后，重置结束日的最小日期
            end.start = datas //将结束日的初始值设定为开始日
        }
    };

    var end = {
        min: laydate.now()
        , max: '2099-06-16 23:59:59'
        , istoday: false
        , choose: function (datas) {
            start.max = datas; //结束日选好后，重置开始日的最大日期
        }
    };
});

layui.use('layer', function () {
    var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
    //触发事件
    var active = {
        update: function (othis) {
            //弹出即全屏
            var index = layer.open({
                type: 2,
                title: "修改商品",
                shadeClose: true,
                content: '/admin/getupdategoods?goodsid='+othis.goodsid,
                area: ['100%', '100%'],
                maxmin: false
            });
            layer.full(index);
        },
        notice: function (date) {
            //示范一个公告层
            layer.open({
                type: 1,
                title: false, //不显示标题栏
                closeBtn: false,
                area: '300px;',
                shade: 0.8,
                id: 'LAY_layuipro', //设定一个id，防止重复弹出
                btn: ['绝不后悔', '我怂了'],
                moveType: 1, //拖拽模式，0或者1,
                content: '<div style="padding: 30px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">删除后将不可恢复，是否确认删除<br></div>',
                yes: function (index, layero) {
                    $.ajax({
                        url: "delgoods.action",
                        type: 'post',
                        data: {
                            goodsid: date.goodsid
                        },
                        dataType: 'json',
                        success: function (data) {
                            if (data == "删除成功！") {
                                window.location.href = "/admin/purgoodslist";
                            }
                            layer.msg(data);
                        }
                    });
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
            });
        }
    };
    $('.updategoods').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.goodsid = $(this).attr('goodsid');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.delgoods').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.goodsid = $(this).attr('goodsid');
        active[method] ? active[method].call(this, othis) : '';
    });
});

layui.use(['laypage', 'layer'], function(){
    var laypage = layui.laypage,layer = layui.layer;

    laypage({
        cont: 'fullpage',
        pages: $('#pageCount').val(), //分页总数
        skip: true,
        curr: $('#pageIndex').val(),
        jump: function(obj, first) {
            if (first != true) {//是否首次进入页面
                var pageIndex = obj.curr;//获取点击的页码
                var jsonObject = $('#jsonObject').val();
                if (jsonObject.length > 0) {
                    console.log(jsonObject);

                    var objs = eval('(' + jsonObject + ')');//将json转成对象
                    var condition = 'pageIndex=' + pageIndex + '&';
                     condition += "querytype=" + objs.type + '&' + "querygoodsname=" + objs.goodname + '&' + "price_min=" + objs.price_min + '&' + "price_max=" + objs.price_max;
                    console.log(condition);
                    window.location.href = "/admin/querygoods?" + condition;
                } else {
                    window.location.href = "/admin/purgoodslist?pageIndex=" + pageIndex;
                }
            }
        }
    });
});
// $(function () {
//     var addgoodstype=$('#addgoodstypetxt').val();
//
//     $("#addgoodstypebtn").click(function () {
//         var params = $.param({
//             'addgoodstype':addgoodstype,
//         },true);
//         $.ajax({
//             url: '/addgoodstype.action',
//             type: 'post',
//             data: params,
//             dataType: 'json',
//             success: function (data) {
//                 if(data=='0') {
//                     window.location.href = "/admin/purgoodslist";
//                 }
//             }
//         });
//
//     }).change();
// })
layui.upload({

});
